﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.BUS;
using QuanLyBanHang.VO;
using QuanLyBanHang.DAL;

namespace QuanLyBanHang
{
    public partial class Form_HD : Form
    {
        HoaDon_VO hd;
        HoaDon_BUS hd_bus;
        SanPham_DAL sp_dal;
        NhanVien_VO nv = new NhanVien_VO();
        public Form_HD(NhanVien_VO NV)
        {
            InitializeComponent();
            hd = new HoaDon_VO();
            sp_dal = new SanPham_DAL();
            hd_bus = new HoaDon_BUS();
            nv = NV;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = hd.Items;
            dataGridView1.Columns[0].HeaderText = "Mã sản phẩm";
            dataGridView1.Columns[1].HeaderText = "Tên sản phẩm";
            dataGridView1.Columns[2].HeaderText = "Số lượng";
            dataGridView1.Columns[3].HeaderText = "Giá";
            dataGridView1.Columns[4].HeaderText = "Tổng giá";

        }

        public void BindGrid()
        {
           
            BindingSource grsource = new BindingSource();
            grsource.DataSource = hd.Items;            
            dataGridView1.DataSource = grsource;
            dataGridView1.Columns[0].ReadOnly = true;
            dataGridView1.Columns[1].ReadOnly = true;
            dataGridView1.Columns[3].ReadOnly = true;
            dataGridView1.Columns[4].ReadOnly = true;
        }
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form f_KH = new Form_KH();
            f_KH.Show();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form f_SP = new Form_SP();
            f_SP.Show();
        }

        private void button_xacnhan_Click(object sender, EventArgs e)
        {
            try
            {
                panel1.Enabled = false;
                hd.MaKH = int.Parse(textBox_kh.Text);
            }
            catch
            {
                MessageBox.Show("Có lỗi");
                panel1.Enabled = true;
            }
        }

        private void button_sp_Click(object sender, EventArgs e)
        {
            try
            {
                int masp = int.Parse(textBox_masp.Text);
                
                CartItem c = new CartItem(masp, sp_dal.getNameByID(masp), 1, sp_dal.getPrice(masp));
                hd.AddCart(c);
                //MessageBox.Show(hd.Items[0].TenSP.ToString());
                BindGrid();
            }
            catch
            {
                MessageBox.Show("Xảy ra lỗi! Vui lòng thử lại");
            }
            
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {

            try
            {
                if (int.Parse(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) <=0)
                    dataGridView1.Rows.RemoveAt(e.RowIndex);                               
            }
            catch
            {
                MessageBox.Show("Xảy ra lỗi! Vui lòng thử lại");
            }
        }

        private void dataGridView1_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            //MessageBox.Show(hd.Items.Count.ToString());
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            panel1.Enabled = true;            
            hd = new HoaDon_VO();
            BindGrid();
        }

        private void button_ok_Click(object sender, EventArgs e)
        {
            try
            {
                hd.MaNV = nv.MaNV;
                hd.Clean();
                if (hd.Items.Count > 0)
                {
                    if (hd_bus.insert(hd)) MessageBox.Show("Thành công");
                    else MessageBox.Show("Đã xảy ra lỗi");
                }
                else
                {
                    MessageBox.Show("Chưa có sản phẩm nào được mua");
                }
            }
            catch
            {
                MessageBox.Show("Đã xảy ra lỗi");
                panel1.Enabled = true;
            }

        }

        private void button_exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //e.ThrowException = true;
            MessageBox.Show("Vui lòng nhập lại");
            
            
        }
    }
}
